<template>
  <div class="rank" ref="rank">
    <scroll :data="toplist" class="toplist" ref="toplist">
      <ul>
        <li class="item" v-for="item in toplist" :key="item.topId+99" @click="selectItem(item)">
          <div class="icon"><img width="100" height="100" v-lazy="item.frontPicUrl" alt=""></div>
          <ul class="songlist">
            <li class="song" v-for="(song,index) in item.song" :key="song.songId+index">
              <span>{{index + 1}}</span>
              <span>{{song.title}}-{{song.singerName}}</span>
            </li>
          </ul>
        </li>
      </ul>
    </scroll>
    <div class="loading-container" v-show="!toplist.length">
      <loading></loading>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
import {getTopList} from 'api/rank'
import {ERR_OK} from 'api/config'
import Loading from 'base/loading/loading'
import Scroll from 'base/scroll/scroll'
import {playlistMixin} from 'common/js/mixin'
import {mapMutations} from 'vuex'

export default {
  name: 'rank',
  data () {
    return {
      toplist: []
    }
  },
  methods: {
    handlePlaylist (playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.rank.style.bottom = bottom
      this.$refs.toplist.refresh()
    },
    ...mapMutations({
      setToplist: 'SET_TOPLIST'
    }),
    selectItem (item) {
      this.$router.push(`/rank/${item.topId}`)
      this.setToplist(item)
    },
    _getTopList () {
      getTopList().then(resp => {
        if (resp.data.code === ERR_OK) {
          let toplists = resp.data.req_0.data.group
          let results = []
          toplists.forEach(item => {
            results.push(...item.toplist)
          })
          this.toplist = results.filter(item => {
            return item.title !== 'MV榜'
          })
        }
      })
    }
  },
  mixins: [playlistMixin],
  created () {
    this._getTopList()
  },
  components: {Scroll, Loading}
}
</script>

<style lang="less" scoped>
  @import "~common/css/variable";
  @import "~common/css/mixin";
  .rank {
    position: fixed;
    width: 100%;
    top: 88px;
    bottom: 0;
    .toplist {
      height: 100%;
      overflow: hidden;
      .item {
        display: flex;
        margin: 0 20px;
        padding-top: 20px;
        height: 100px;
        &:last-child {
          padding-bottom: 20px;
        }
        .icon {
          flex: 0 0 100px;
          width: 100px;
          height: 100px;
        }
        .songlist {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: center;
          padding:0 20px;
          height: 100px;
          overflow: hidden;
          background-color: @color-highlight-background;
          color: @color-text-d;
          font-size: @font-size-small;
          .song {
            .no-wrap;
            line-height: 26px;
          }
        }
      }
    }
    .loading-container {
      position: absolute;
      width: 100%;
      top: 50%;
      transform: translateY(-50%);
    }
  }
</style>
